<?php
class OrderAction extends CommonAction {
	
	function _initialize(){
		parent::_initialize();
	}
	
    function index(){
    	$status = $this->_param('status');
    	$order_number = $this->_param('order_number');
    	$user_id = $this->_param('user_id');
    	$map = array();
    	
    	if($status != '')
    		$map['order_status'] = $status;
    	if($order_number != '')
    		$map['order_number'] = $order_number;
    	if($user_id != '')
    		$map['user_id'] = $user_id;
    	
    	$model = D('Order');
    	$count = $model->where($map)->count('order_id');
    	
    	if ($count > 0) {
    		import("ORG.Util.Page");
    		
    		//分页查询数据
    		$p = new Page($count, 20);
    		$voList = $model->where($map)->relation('member')->order("`order_id` desc")->limit($p->firstRow . ',' . $p->listRows)->select();
    		//分页跳转的时候保证查询条件
    		foreach ($map as $key => $val) {
    			if (!is_array($val)) {
    				$p->parameter .= "$key=" . urlencode($val) . "&";
    			}
    		}
    		
    		//分页显示
    		$page = $p->show();
    		//模板赋值显示
    		$this->assign('status_arr', D('Order')->get_status_arr());
    		$this->assign('list', $voList);
    		$this->assign("page", $page);
    	}
    	cookie('_currentUrl_', __SELF__);
    	$this->display();
    }
    
    function detail(){
    	//0 未确认 1 已确认 2 收到货款 3 成功订购 4 已发货 5 已完成 9 已删除
    	$order_id = $this->_request('id', 'intval');
    	$model = D('Order');
    	$order_info = $model->where(array('order_id'=>$order_id))->relation(true)->find($order_id);
    	
    	if(!$order_info)
    		$this->error('参数失败!');
    	
    	
    	$this->assign('status_arr', $model->get_status_arr());
    	$this->assign('order_info', $order_info);
    	
    	$this->display();
    }
    
    function deal(){
    	//0 未确认 1 已确认 2 收到货款 3 成功订购 4 已发货 5 已完成 9 已删除
    	$order_id = $this->_request('id', 'intval');
		$model = D('Order');
		$order_info = $model->where(array('order_id'=>$order_id))->relation(true)->find($order_id);
		
		if(!$order_info)
			$this->error('参数失败!');
		
		//获取可转变的状态
		$status_arr = $model->get_next_status_arr( $order_info['order_status'] );

		if($this->isPost()){
			/*
			 * 1. 更新商品表中的数量
			 * 2. 更新快递费用、订单总金额、状态
			 * 3. 站内信通知
			 * 4. 若订单完成，则更新用户表中的成功交易次数和交易金额
			 */
			
			$status = $this->_post('status');
			$admin_memo = $this->_post('admin_memo', 'trim');
			$goods_id_arr = $this->_post('goods_id');
			$new_num_arr = $this->_post('new_num');
			$transpost_amount = $this->_post('transpost_amount');
			$amount_check = $this->_post('amount_check');
			$weight = $this->_post('weight');
			
			//判断状态流转
			if( !isset($status_arr[$status]) )
				$this->error('订单处理失败!status error');
				
			$model->startTrans();
			
			
			//1. 更新商品表中的数量
			$message_model = D('message');
			
			if($order_info['order_status'] == '0'){
				$goods_model = D('goods_info');
				
				$i = 0;
				foreach ($order_info['goods_info'] as $goods_info){
					if($goods_info['goods_id'] != $goods_id_arr[$i])
						$this->error('订单处理失败!goods_id not match!');
					
					if($goods_info['num_check'] == $new_num_arr[$i]){
						$i++;
						continue;
					}
						
					$flag = $goods_model->where("goods_id = '{$goods_info['goods_id']}'")->save(array('num_check' => $new_num_arr[$i]));
					
					if(!$flag){
						$model->rollback();
						$this->error('订单处理失败!goods_info update error!');
					}
	
					$i++;
				}
			}
			
			//2. 更新快递费用、订单总金额、状态
			$op_msg = date('Y-m-d H:i').' 订单['.$order_info['order_number'].']状态变更为：'.$status_arr[$status];
			
			$order_arr = array(
					'order_status' => $status,
					'admin_memo' => $admin_memo,
					'operation_log' => $order_info['operation_log'].$op_msg."\n",
			);
			$other_info = array();
			if($order_info['order_status'] == '0'){
				$other_info = array(
						'amount_check' => $amount_check,
						'amount_thb_check' => $amount_check * $order_info['exchange_rate'],
						'transpost_amount' => $transpost_amount,
						'transpost_amount_thb' => $transpost_amount * $order_info['exchange_rate'],
						'check_time' => date('YmdHis'),
				);
			}
			
			if($order_info['order_status'] == '3'){
				$other_info = array(
						'weight' => $weight,
				);
			}
			
			$order_arr = array_merge($order_arr, $other_info);
			
			$flag = $model->where("order_id = '$order_id'")->save($order_arr);
			
			if(!$flag){
				$model->rollback();
				$this->error('订单处理失败!order_info update error!');
			}
			
			//4. 若订单完成，则更新用户表中的成功交易次数和交易金额
			if($status == '5'){
				$user = D('member');
				$user->find($order_info['user_id']);
				
				if(!$user){
					$model->rollback();
					$this->error('订单处理失败!member update error1!');
				}
				
				$user->trans_num += 1;
				$user->trans_amount +=  $order_info['amount_check']+$order_info['transpost_amount'];
				$flag = $user->save();
				
				if(!$flag){
					$model->rollback();
					$this->error('订单处理失败!member update error2!');
				}
			}
			
			
			//3. 站内信通知
			$flag = $message_model->add(array(
					'send_user' => session('login_user_id'),
					'rece_user' => $order_info['user_id'],
					'title' => $op_msg,
					'content' => $op_msg,
					'add_time' => date('YmdHis'),
					'is_read' => 0,
			));
			
			if(!$flag){
				$model->rollback();
				$this->error('订单处理失败!message add error!');
			}
			$model->commit();
			
			$this->success('订单处理成功!',cookie('_currentUrl_'));
		}
		
		
		$this->assign('status_arr', $status_arr);
		$this->assign('order_info', $order_info);
		$this->display();
    }
}